<?php
class model_NewsMapper {
    private $table = 'news';

    public function  __construct() {
        model_ConfigMapper::connect();
    }

    public function getTable() {
        return $this->table;
    }

    public function find($id) { //находим сообщение, записываем его содержание в свойства объекта
        $query = 'SELECT * from '. $this->table .' WHERE id = '. $id;
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);

        if ($row) {
            $m = new model_NewsModel($row);
        } else {
            $m = new model_NewsModel(null);
        }

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        return $m;
    }

    public function fetchAll($start, $nums) { //
        $query = 'SELECT * from '. $this->table .' ORDER BY id DESC LIMIT '. $start .', '. $nums ;
        $result = mysql_query($query);

        $entries = array();

        while ($row = mysql_fetch_assoc($result)) {
            $entry = new model_NewsModel($row);
            $entries[] = $entry;
        }

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        return $entries;
    }

    public function fetchByItem($table, $item, $id) { //
        $query = 'SELECT * from '. $table .' WHERE '. $item .' = '. $id;
        $result = mysql_query($query);

        $entries = array();

        while ($row = mysql_fetch_assoc($result)) {
            if('tagid' == $item) {
                $query = 'SELECT * from '. $this->table .' WHERE id = '. $row['newsid'];
                $res = mysql_query($query);
                $r = mysql_fetch_assoc($res);

                $timer = model_ConfigMapper::instance();
                $timer->setSqlCounter();

            } else {
                $r = $row;
            }
            $entry = new model_NewsModel($r);
            $entries[] = $entry;
        }
        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        return $entries;
    }

    public function save($model) {

        if (null == $model->getId()) {
            //сохраняем новую 
            $query = 'INSERT INTO '.$this->table.' (title, content, author, date, img) VALUES ("'. $model->getTitle() .'","'. $model->getContent() .'","'. $model->getAuthor() .'","'. $model->getDate() .'","'. $model->getImg() .'")';
        } else {
            //заменяем строку строкой
            $query = 'UPDATE '.$this->table.' SET title="'. $model->getTitle() .'", content="'. $model->getContent() .'", img="'. $model->getImg() .'" WHERE id="'. $model->getId() .'"' ;
        }
        $result = mysql_query($query);

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();
    }

    public function delete($id) {// собственно удаляем
        $query = 'DELETE FROM '.$this->table.' WHERE id="'.$id.'"';
        $result = mysql_query($query);

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        $query = 'DELETE FROM tagtonews WHERE newsid="'.$id.'"';
        $result = mysql_query($query);

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();

        $query = 'DELETE FROM comments WHERE parentid="'.$id.'"';
        $result = mysql_query($query);

        $timer = model_ConfigMapper::instance();
        $timer->setSqlCounter();
    }

    public function checkMsg($msg) {
        $message = '';
        if ('' == $msg['title']) {
            $message .= 'Не указан заголовок новости! ';
        }
        if ('' == $msg['content']) {
            $message .= 'Содержание новости не указано! ';
        }
        if (!empty($message)) {
            return $message;
        }
    }
}